﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace FIS.iDET.TemplateCreator
{
    public class RegistryService
    {
        private static RegistryKey appRegistryKey =  Registry.CurrentUser.OpenSubKey(GlobalConstants.REGISTRY_PATH, true);
        private static RegistryKey appRegistryLicenseKey = Registry.LocalMachine.OpenSubKey(GlobalConstants.REGISTRY_PATH, false);

        public static void Init()
        {
            // in case key does not exist
            if (appRegistryKey == null)
            {
                // create new one
                Registry.CurrentUser.CreateSubKey(GlobalConstants.REGISTRY_PATH);

                appRegistryKey = Registry.CurrentUser.OpenSubKey(GlobalConstants.REGISTRY_PATH, true);
            }
            try
            {
                if (appRegistryLicenseKey == null)
                {
                    Registry.LocalMachine.CreateSubKey(GlobalConstants.REGISTRY_PATH);
                    appRegistryLicenseKey = Registry.LocalMachine.OpenSubKey(GlobalConstants.REGISTRY_PATH, true);
                }
            }
            catch 
            {
                MessageBox.Show("Your application hasn't been activated. Please run under administrative rights to active it!","Administrator Request",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                Application.Exit();
            }
        }

        /// <summary>
        /// gets application registry value from a key
        /// </summary>
        /// <param name="name">key name</param>
        /// <returns>key's value</returns>
        public static string GetValue(string name)
        {
            try
            {
                if (name == GlobalConstants.REGISTRY_SERIAL_NUMBER_KEY || name == GlobalConstants.REGISTRY_USER_NAME)
                    return appRegistryLicenseKey.GetValue(name) as string;
                else
                return appRegistryKey.GetValue(name) as string;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while reading from registry key \"" + appRegistryKey.ToString() + "\": " + ex.Message);
                return null;
            }

        }

        /// <summary>
        /// set application registry value to a key
        /// </summary>
        /// <param name="name">key name</param>
        /// <param name="value">new value to set to key</param>
        public static void SetValue(string name, string value)
        {
            try
            {
                if (name == GlobalConstants.REGISTRY_SERIAL_NUMBER_KEY || name == GlobalConstants.REGISTRY_USER_NAME)
                {                    
                    //grant write permisson
                    appRegistryLicenseKey = Registry.LocalMachine.OpenSubKey(GlobalConstants.REGISTRY_PATH, true);
                    appRegistryLicenseKey.SetValue(name, value);
                    appRegistryLicenseKey.Flush();
                }
                else
                {
                    appRegistryKey.SetValue(name, value);
                    appRegistryKey.Flush();
                }
            }
            catch 
            {
                MessageBox.Show("Error while writing to registry key. Please call IT for support.", "Administrative Rights Needed",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }
    }
}
